using ADODB;
using System;
using System.IO;
using System.Xml;
using System.Collections;
using Microsoft.CommerceServer.Interop.Catalog;
using Microsoft.CommerceServer.Interop.Configuration;

namespace CatalogCopy
{
	/// <summary>
	/// Summary description for Util.
	/// </summary>
	public class Util
	{
		/// <summary>
		/// Defualt Constructor
		/// </summary>
		public Util()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// Fix the exported catalog for copy.
		/// </summary>
		/// <param name="objXML"></param>
		/// <param name="strDestCatalogName"></param>
		private static void PatchXML(XmlDocument objXML, string strDestCatalogName)
		{
			XmlNode nodeCatalog = objXML.DocumentElement.SelectSingleNode("/MSCommerceCatalogCollection/Catalog");
			XmlNodeList nodeListRelationships = objXML.DocumentElement.SelectNodes("//Relationship");

			XmlNode attr = objXML.CreateNode(XmlNodeType.Attribute, "name", null);
			attr.Value = strDestCatalogName;

			nodeCatalog.Attributes.SetNamedItem(attr);
			IEnumerator currentNode = nodeListRelationships.GetEnumerator();

			while(currentNode.MoveNext())
			{
				attr = objXML.CreateNode(XmlNodeType.Attribute, "targetCatalog", null);
				attr.Value = strDestCatalogName;
				
				((XmlNode)currentNode.Current).Attributes.SetNamedItem(attr);
			}
		}

		/// <summary>
		/// Copy catalog sSourceCatalog as sDestCatalog for a given site.
		/// </summary>
		/// <param name="sSourceCatalog"></param>
		/// <param name="sDestCatalog"></param>
		/// <param name="sSiteName"></param>
		/// <returns></returns>
		public static bool CreateCatalog(string sSourceCatalog, string sDestCatalog, string sSiteName)
		{
			// Set private variables
			string strSourceCatalog = sSourceCatalog;
			string strDestCatalog = sDestCatalog;

			CatalogManager objCatMgr = new CatalogManagerClass();
			XmlDocument objXML = new XmlDocument();
			string strSourceFile = Path.GetTempFileName() + "_" + strSourceCatalog + ".xml";
    
			objCatMgr.Initialize(sSiteName, false, null);
			objCatMgr.ExportXML(strSourceFile, strSourceCatalog, true, CatalogXMLFmtEnum.cscFmtElementCentric, "En-US");
    
			XmlTextReader xmlread = new XmlTextReader(strSourceFile);
			objXML.Load(xmlread);
    
			string strDestFile = Path.GetTempFileName() + "_" + strDestCatalog + ".xml";

			// Resetting instances of catalog name in XML
			PatchXML(objXML, strDestCatalog);

			// Writing out transformed XML to destination file
			objXML.Save(strDestFile);

			// Importing new catalog ...
			objCatMgr.ImportXML(strDestFile, false, true, true);

			// try to clean up
			try
			{
				File.Delete(strSourceFile);
				File.Delete(strDestFile);
			}
			catch{}
    
			return true;
		}
		
		/// <summary>
		/// Get a list of Commerce site
		/// </summary>
		/// <returns></returns>
		public static _Recordset GetCommerceSites()
		{
			GlobalConfig2 oglbConfig = new GlobalConfig2Class();
			oglbConfig.Initialize(null);
			return oglbConfig.Sites();
		}

		/// <summary>
		/// Get a list of catalogs for a given site
		/// </summary>
		/// <param name="sitename"></param>
		/// <returns></returns>
		public static _Recordset GetCatalogs(string sitename)
		{
			if(sitename!=string.Empty)
			{
				CatalogManager ctlgMgr = new CatalogManagerClass();
				ctlgMgr.Initialize(sitename, false, null);
				object dummy = new object();
				return ctlgMgr.get_Catalogs(null, null, null, null, ref dummy);
			}
			return null;
		}

		/// <summary>
		/// Delete a catalog given the site name and catalog
		/// </summary>
		/// <param name="siteName"></param>
		/// <param name="catalogName"></param>
		public static void DeleteCatalog(string siteName, string catalogName)
		{
			CatalogManager ctlgMgr = new CatalogManagerClass();
			ctlgMgr.Initialize(siteName, false, null);
			ctlgMgr.DeleteCatalog(catalogName);
		}
	}
}
